const express = require("express")
const router = express.Router()
const { default: mongoose } = require("mongoose")
const { readFile, wtiteFile } = require("../util/filedata")

// 获取schema
const { Schema } = mongoose
const url = "mongodb://127.0.0.1:27017/shopApp"
mongoose.connect(url).then(() => {
    // console.log("链接成功数据库")
})

// 创建数据模版
const dataListSchema = new Schema({
    data: { type: Array }

})
const tjData = mongoose.model("tjdata", dataListSchema)
const nzData = mongoose.model("nzdata", dataListSchema)
const shoeData = mongoose.model("tjdata", dataListSchema)
const manData = mongoose.model("nzdata", dataListSchema)
const scData = mongoose.model("scpdata", dataListSchema)
const zbData = mongoose.model("zbdata", dataListSchema)
const jfData = mongoose.model("jfdata", dataListSchema)
const foodData = mongoose.model("fooddata", dataListSchema)
const dqData = mongoose.model("dqdata", dataListSchema)
const bhData = mongoose.model("bhdata", dataListSchema)
const mzData = mongoose.model("mzdata", dataListSchema)
const detailData = mongoose.model("addData", dataListSchema)
// 将json数据保存到数据库
// async function saveBannerData() {
//     // 读取数据Hometextiles
//     let getDataList = JSON.parse(await readFile("./data/recommend.json", "utf8"));
//     console.log("111", getDataList)
//     // 保存到数据库
//     let result = await dataList.create({
//         data: getDataList
//     });
//     console.log("保存到数据库", result);
//     // let result = product.find({}, (res) => {
//     //     console.log('用户列表', res);
//     // })
// }
// 仅执行一次
// saveBannerData();


// 母婴数据
router.get("/getMy", async (req, res, next) => {
    try {
        let data = await nzData.find({})
        console.log("母婴数据", data[0]);
        // data.fn(0)  // 报错
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})

// 鞋包数据
router.get("/getShoe", async (req, res, next) => {
    try {
        let data = await shoeData.find({})
        console.log("鞋包数据", data[0]);
        // data.fn(0)  // 报错
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})

// 男装数据
router.get("/getNz", async (req, res, next) => {
    try {
        let data = await manData.find({})
        console.log("男装数据", data[0]);
        // data.fn(0)  // 报错
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})

// 奢品数据
router.get("/getSp", async (req, res, next) => {
    try {
        let data = await scData.find({})
        console.log("奢品数据", data[0]);
        // data.fn(0)  // 报错
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})

// 珠宝数据
router.get("/getZb", async (req, res, next) => {
    try {
        let data = await zbData.find({})
        console.log("珠宝数据", data[0]);
        // data.fn(0)  // 报错
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})

// 家纺数据
router.get("/getJf", async (req, res, next) => {
    try {
        let data = await jfData.find({})
        console.log("家纺数据", data[0]);
        // data.fn(0)  // 报错
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})

// 食品数据
router.get("/getFood", async (req, res, next) => {
    try {
        let data = await foodData.find({})
        console.log("食品数据", data[0]);
        // data.fn(0)  // 报错
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})


// 电器数据
router.get("/getDq", async (req, res, next) => {
    try {
        let data = await dqData.find({})
        console.log("电器数据", data[0]);
        // data.fn(0)  // 报错
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})


// 女装数据
// router.get("/getNz", async (req, res, next) => {
//     try {
//         let data = await dataList.find({})
//         console.log("女装数据", data[0]);
//         // data.fn(0)  // 报错
//         return res.status(200).json({
//             msg: "okok",
//             code: 200,
//             data: data
//         })
//     }
//     catch (err) {
//         return next({
//             code: 1001,
//             error: err.message,
//             errDic: 'hhhhhhhhh'
//         })
//     }
// })

// 推荐数据 
router.get("/getTj", async (req, res, next) => {
    try {
        let data = await dataList.find({})
        console.log("推荐数据", data[0]);
        // data.fn(0)  // 报错
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})

// 百货数据 
router.get("/getBh", async (req, res, next) => {
    try {
        let data = await bhData.find({})
        console.log("推荐数据", data[0]);
        // data.fn(0)  // 报错
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})

// 美妆数据
router.get("/getMz", async (req, res, next) => {
    try {
        let data = await mzData.find({})
        console.log("美妆数据", data[0]);
        // data.fn(0)  // 报错
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})

module.exports = router